package com.xxl.sodmall.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.support.v7.app.AlertDialog
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import com.dgshanger.sanhxiaofeisc.App.Companion.application
import com.dgshanger.sanhxiaofeisc.R
import kotlinx.android.synthetic.main.dialog_pay.view.*

/**
 * ChooseImageDialogWrapper
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/3/16 15:46.
 */

class WalletPayDialogWrapper(context: Context, money: String) {

    private var payDialog: AlertDialog? = null

    /**
     * 显示选择图片的对话框
     */
    fun showDialog() {
        payDialog?.show()
    }

    /**
     * 隐藏对话框
     */
    fun dismissDialog() {
        if (payDialog != null && payDialog!!.isShowing()) {
            payDialog?.dismiss()
        }

    }

    fun setOnPasswordFinishListener(onPasswordFinishListener: OnPasswordFinishListener): WalletPayDialogWrapper {
        this.onPasswordFinishListener = onPasswordFinishListener
        return this
    }

    private var onPasswordFinishListener: OnPasswordFinishListener? = null


    interface OnPasswordFinishListener {
        /**
         * 支付密码输入完成监听
         */
        fun onPasswordFinishListener(password: String)
    }

    @SuppressLint("SetTextI18n")
    fun setIntegralText(integral :String){
        contentView.tv_pay_integral.text = "${application.getString(R.string.integral)} $integral"
        contentView.tv_pay_integral.visibility=View.VISIBLE
    }

    private lateinit var contentView : View
    init {
        if (payDialog == null) {
            contentView = LayoutInflater.from(context).inflate(
                    R.layout.dialog_pay, null) as View
            contentView.tv_pay_money.setText("${context.getString(R.string.Cash)} $money")
            contentView.et_password.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                    if (contentView.et_password.length() == 6 && onPasswordFinishListener != null) {
                        onPasswordFinishListener?.onPasswordFinishListener(contentView.et_password.text.toString())
                    }
                }

            })
            payDialog = AlertDialog.Builder(context)
                    .setView(contentView)
                    .create()
            val onClickListener = View.OnClickListener { v ->
                when (v.id) {
                    R.id.iv_close -> dismissDialog()
                }
            }

            contentView.findViewById<View>(R.id.iv_close).setOnClickListener(onClickListener)

//            val window = chooseImageDialog!!.window
//            if (window != null) {
//                window.setGravity(Gravity.BOTTOM)//设置窗口位于底部
//                window.getDecorView().setPadding(0, 0, 0, 0)
//                val lp = window.getAttributes()
//                lp.width = WindowManager.LayoutParams.MATCH_PARENT
//                lp.height = WindowManager.LayoutParams.WRAP_CONTENT
//                window.setAttributes(lp)
//            }
        }
    }
}
